package com.entfrm.system.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.entfrm.base.api.R;

import com.entfrm.security.entity.EntfrmUser;
import com.entfrm.security.util.SecurityUtil;
import com.entfrm.system.entity.BiInfo;
import com.entfrm.system.entity.BiLogin;

import com.entfrm.system.service.BiInfoService;
import com.entfrm.system.service.BiLoginService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@AllArgsConstructor
@RequestMapping( "/biReport")
public class BiReportController {
    private final BiInfoService biInfoService;
    private final BiLoginService biLoginService;

    private QueryWrapper<BiInfo> getQueryWrapper(BiInfo biInfo) {
        return new QueryWrapper<BiInfo>()
                .like(StrUtil.isNotBlank(biInfo.getBiName()), "bi_name", biInfo.getBiName())
                .orderByAsc("sort_by");

    }
    private QueryWrapper<BiLogin> getQueryWrapper(BiLogin biLogin) {
        LocalDateTime startOfDay = LocalDate.now().atStartOfDay();
        LocalDateTime endOfDay = LocalDate.now().atTime(23, 59, 59);
        return new QueryWrapper<BiLogin>()
                .between("login_time",startOfDay,endOfDay)
                .orderByAsc("login_time");
    }

    @GetMapping("/biInfo")
    public R list(Page page, BiInfo biInfo) {
        EntfrmUser user= SecurityUtil.getUser();
        biInfo.setUserId(user.getId().toString());
         List<BiInfo> list = biInfoService.getBiInfoByUser(biInfo);
        return R.ok(list,list.size() );

    }
    @GetMapping("/biLogInfo")
    public R list(Page page,BiLogin biLogin) {
        IPage<BiLogin> biInfoPage = biLoginService.page(page, getQueryWrapper(biLogin));
        return R.ok(biInfoPage.getRecords(), biInfoPage.getTotal());

    }
}
